import { BasicColumn, FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { DescItem } from '/@/components/Description/index';
import { listDataGroup } from '/@/api/infra/dataGroup';
import { saveOrUpdateGroup, deleteGroup, batchDeleteGroup } from './config.api';
import { DataGroupBizType } from "/@/components/BizDataGroup/index"

export const columns: BasicColumn[] = [
  {
    title: '配置名称',
    width: 160,
    dataIndex: 'configName',
  },
  {
    title: '配置键名',
    width: 150,
    dataIndex: 'configKey',
  },
  {
    title: '配置组',
    dataIndex: 'groupName',
    width: 120
  },
  {
    title: '内置',
    width: 60,
    dataIndex: 'sysBuiltIn',
    customRender: ({text}) => {
      const color = !text ? 'red' : 'green';
      const showText = text ? '是' : '否';
      return render.renderTag(showText ,color)
    }
  },

  {
    title: '创建时间',
    width: 150,
    dataIndex: 'createdAt',
  },
  {
    title: '创建者',
    width: 90,
    dataIndex: 'createdBy',
  }
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'keyWord',
    label: '配置名',
    componentProps: {
      placeholder: '配置名称/键值',
    },
    component: 'Input',
    // colProps: {span: 6},
  },
  {
    label: '配置组',
    field: 'groupId',
    component: 'ApiSelect',
    componentProps: {
      optionFilterProp: "label",
      labelField: 'name',
      valueField: 'id',
      showSearch: true,
      api: listDataGroup,
      params: {
        bizType: DataGroupBizType.CONFIG,
      },
    },
    // colProps: {span: 6},
  },
  {
    label: '系统内置',
    field: 'sysBuiltIn',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'config_type',
      stringToBool: true,
      placeholder:'请选择系统内置'
    },
    // colProps: {span: 6},
  }
];

export const formDetailSchema : DescItem[] = [
  {
    field: 'configName',
    label: '配置名称'
  },
  {
    field: 'configKey',
    label: '配置键名'
  },
  {
    field: 'groupName', 
    label: '配置组'
  },
  {
    field: 'configValue',
    label: '配置值'
  },
  {
    field: 'sysBuiltIn', 
    label: '系统内置',
    render: (curVal, data) => {
      const color = !curVal ? 'red' : 'green';
      const showText = curVal ? '是' : '否';
      return render.renderTag(showText ,color)
    },
  },
  {
    field: 'createdAt',
    label: '创建时间'
  },
  {
    field: 'createdBy',
    label: '创建者'
  },
  {
    label: '备注',
    labelMinWidth: 60,
    field: 'remark',
  }
  
];


export const configFormSchema: FormSchema[] = [
  {
    label: 'id',
    field: 'id',
    component: 'Input',
    show: false
  },
  {
    field: 'configName',
    label: '配置名称',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入配置名称',
    }
  },
  {
    field: 'configKey',
    label: '配置键名',
    component: 'Input',
    required: true,
    componentProps: {
      placeholder: '请输入参数键名',
    },
    dynamicDisabled: ({values}) => {
      return !!values.id;
    }
  },
  {
    label: '配置组',
    field: 'groupId',
    component: 'MySelectDataGroup',
    componentProps: {
      labelField: 'name',
      valueField: 'id',
      bizType: DataGroupBizType.CONFIG,
      deleteApi: deleteGroup,
      // 保存或者更新api
      saveOrUpdateApi: saveOrUpdateGroup,
      // 批量删除api
      batchDeleteApi: batchDeleteGroup,
      title: '配置组',
      maxSelectCount: 1
    },
    // colProps: {span: 6},
  },
  {
    field: 'sysBuiltIn',
    label: '系统内置',
    required: true,
    component: 'JDictSelectTag',
    defaultValue: true,
    componentProps: {
      type:'radio',
      stringToBool: true,
      dictCode: 'config_type',
      placeholder:'请选择类型',
    },
  },
  {
    field: 'configValue',
    label: '参数配置',
    component: 'InputTextArea',
    required: true,
    colProps: {span: 24},
    componentProps: {
      autoSize: {
        minRows: 5,
        maxRows: 5
      }
    },
  },

  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    componentProps: {
      autoSize: {
        minRows: 5,
        maxRows: 5
      }
    },
    required: false
  },
]
